﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CuteEditor;
using Wjtour.Domain;
using Wjtour.BLL;
using Hjp.FrameWork.Common;

namespace Wjtour.Web.Admin.article
{
    public partial class addedi : BasePage
    {
        protected DropDownList cid;
        protected TextBox title;
        protected TextBox img;
        protected TextBox source;
        protected TextBox keyword;
        protected TextBox desc;
        protected TextBox sort;
        protected Editor content;
        protected Button submit;
        protected string type;

        protected int id = 0;
        protected int categoryid = 0;
        Wjtour.Domain.article link = new Domain.article();
        article_bll _bll = new article_bll();
        articletype_bll _tbll = new articletype_bll();

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Utils.StrToInt(Request["id"], 0);
            categoryid = Utils.StrToInt(Request["cid"], 0);
            type = Request["type"];
            submit.Click += new EventHandler(submit_Click);
            if (!IsPostBack)
            {
                BindDropdown();
            }
            if (id > 0)
            {
                link = _bll.Load(id);

                if (!IsPostBack)
                {
                    if (link != null)
                    {
                        title.Text = link.title;
                        cid.SelectedValue = link.cid.ToString();
                        source.Text = link.source;
                        img.Text = link.img;
                        keyword.Text = link.keyword;
                        desc.Text = link.desc;
                        sort.Text = link.sort.ToString();
                        content.Text = link.content;
                    }
                }
            }
            if (categoryid > 0)
            {
                cid.SelectedValue = categoryid.ToString();
            }

            if (type == "11")
            {
                title.Text = "咨询中心";
                title.ReadOnly = true;
            }
        }

        void BindDropdown()
        {
            string sql = "select * from articletype";
            if (!string.IsNullOrEmpty(type))
                sql = "select * from articletype where id in (" + type + ")";
            List<articletype> types = _tbll.GetAll<articletype>(sql).ToList();
            if (types != null && types.Count > 0)
            {
                List<articletype> ptypes = types.Where(t => t.pid == 0).ToList();
                foreach (var ptype in ptypes)
                {
                    cid.Items.Add(new ListItem(ptype.title, ptype.id.ToString()));
                    List<articletype> subtypes = types.Where(t => t.pid == ptype.id).ToList();
                    if (subtypes != null && subtypes.Count > 0)
                    {
                        foreach (var item in subtypes)
                        {
                            cid.Items.Add(new ListItem(" ┝ " + item.title, item.id.ToString()));
                        }
                    }
                }
            }
        }

        void submit_Click(object sender, EventArgs e)
        {
            link.id = id;
            link.title = title.Text;
            link.cid = Utils.StrToInt(cid.SelectedValue, 0);
            link.source = source.Text;
            link.img = img.Text;
            link.keyword = keyword.Text;
            link.desc = desc.Text;
            link.sort = Utils.StrToInt(sort.Text, 0);
            link.content = content.Text;
            if (link.id == 0)
                link.addtime = DateTime.Now;

            bool b = _bll.Save(link);
            if (b)
            {
                SaveLog(id == 0 ? "Add" : "edit");
                Utils.Alert(this.Page, "保存成功！");
                Utils.Redirect(this.Page, "list.aspx?type=" + type, false);
            }
        }
    }
}